package yt.demo.demoadmin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import yt.demo.appcommonbean.http.DeleteRequest;
import yt.demo.appcommonbean.http.RestResponse;
import yt.demo.appcommondb.base.BaseEntity;
import yt.demo.appcommondb.dto.SplitPageDTO;
import yt.demo.appcommondb.vo.PageVO;
import yt.demo.appcoresystem.dto.RoleDTO;
import yt.demo.appcoresystem.entity.SystemRole;
import yt.demo.appcoresystem.service.ISystemRoleMenuService;
import yt.demo.appcoresystem.service.ISystemRoleService;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {

    private final ISystemRoleService service;
    private final ISystemRoleMenuService roleMenuService;

    @Autowired
    public RoleController(ISystemRoleService service, ISystemRoleMenuService roleMenuService) {
        this.service = service;
        this.roleMenuService = roleMenuService;
    }

    @GetMapping("/getPageList")
    public RestResponse<PageVO<SystemRole>> getPageList(SplitPageDTO page, SystemRole params) {
        return RestResponse.ok(this.service.getPageList(page, params, w->w.orderByDesc(SystemRole::getId),null));
    }

    @GetMapping("/getList")

    public RestResponse<List<SystemRole>> getList(SystemRole params) {
        return RestResponse.ok(this.service.getList(params,  w->w.orderByDesc(SystemRole::getId),null));
    }

    @GetMapping("/getRoleMenuIds")
    public RestResponse<List<Long>> getRoleMenuIds(@RequestParam("id") Long id) {
        return RestResponse.ok(this.roleMenuService.getRoleMenuIds(List.of(id)));
    }

    @PostMapping("/create")
    public RestResponse<SystemRole> create(@RequestBody @Validated(BaseEntity.CreateGroup.class) SystemRole req) {
        return RestResponse.ok(this.service.create(req));
    }

    @PostMapping("/update")
    public RestResponse<SystemRole> update(@RequestBody @Validated(BaseEntity.UpdateGroup.class) SystemRole req) {
        return RestResponse.ok(this.service.update(req));
    }

    @PostMapping("/syncRoleMenus")
    public RestResponse<Void> updateMenus(@RequestBody @Validated RoleDTO.updateMenus req) {
        return RestResponse.ok(this.service.syncRoleMenus(req));
    }

    @PostMapping("/delete")
    public RestResponse<Void> delete(@RequestBody DeleteRequest req) {
        this.service.delete(req);
        return RestResponse.ok();
    }


}
